<template>
    <div class="mainDiv">
        <div class="rowHead">
            <el-button type="success" icon="plus" @click="addOtherPermission" v-qx="quanxian.add" v-show="noadd">权限等级</el-button>
            <el-button type="primary" icon="h-refresh1" @click="getmsg" style="float:right">刷新列表</el-button>
        </div>
        <el-table :data="tableData" :stripe="true" height="100%">
            <el-table-column prop="gradeid" label="权限等级"></el-table-column>
            <el-table-column prop="remarks" label="等级说明"></el-table-column>
            <el-table-column props="edittime" label="最后修改时间">
                <template slot-scope="scope">
                    {{scope.row.edittime|hourTime}}
                </template>
            </el-table-column>
            <el-table-column label="等级操作">
                <template slot-scope="scope">
                    <el-button-group>
                        <el-button icon="el-icon-h-edit" type="warning" size="mini" @click="editremarks(scope.row)" v-qx="quanxian.edit"></el-button>
                        <el-tooltip content="设置权限" placement="top" :open-delay="800" v-qx="quanxian.set">
                            <el-button icon="el-icon-setting" type="primary" size="mini" @click="setgrade(scope.row.id)"></el-button>
                        </el-tooltip>
                        <el-button icon="el-icon-h-del" type="danger" size="mini" @click="deleteGrade(scope.row.id)" v-qx="quanxian.del"></el-button>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
import C from '#/src/common/common.js'
import Rule from '#/src/shop/rule.js'

export default {
    name: 'permissionLevel',
    data() {
        return {
            tableData: [],
            quanxian: {
                add: 'grade_add',
                edit: 'grade_edit',
                set: 'grade_set',
                del: 'grade_del',
            },
            noadd: true
        }
    },
    methods: {
        addOtherPermission: async function() {
            let res = await Rule.openAddRule()
            if (res && res.data) {
                C.success(res.data)
                this.getmsg()
            }
        },
        getmsg: async function() {
            let data = await C.ajax('/shop/rule/getgradeallot', {}, 'post')
            if (data) {
                this.tableData = data
                if (data.length > 10) {
                    this.noadd = false
                } else {
                    this.noadd = true
                }
            }
        },
        editremarks: async function(row) {
            let res = await Rule.openAddRule(row)
            if (res && res.data) {
                C.success(res.data)
                this.getmsg()
            }
        },
        setgrade: async function(id) {
            let data = await C.openBrowserWindow('/editGrade', { id: id }, { width: 1200, height: 760, useSpa: true, spaModel: 'shop', transparent: true, frame: false })
            if (data) {
                C.success(data.data.info)
                this.getmsg()
            }
        },
        deleteGrade: function(id) {
            C.confirm('确定删除自定义权限？', async() => {
                let data = await C.ajax('/shop/rule/deleteothergrade', { id: id }, 'post')
                if (data) {
                    C.success(data.data)
                    this.getmsg()
                }
            })
        }
    },
    mounted: function() {
        this.getmsg()
    }
}
</script>